<?php 


class Air_model extends CI_Model{
	
	protected $sHashCode="";
	protected $iOilFee=140;
	protected $iAirportFee=50;
	protected $iinter=0;		//是否国际运价
	public $Currencyrate=1;
	public $internationrate=1;
	public $mainCurrency="RMB";
	protected $arAirport=array();
	protected $weblang="english";
	protected $Servercharge=array();
	protected $AgentCode;
	public $db2;
	
	protected $isset_rebate=false;
	protected $Ccommision=array();	//返点设置
	private $wsdl="http://ws.jzinfo.sh.cn/vibe2/vIBEService.asmx?wsdl";
	
	function Air_model()
	{
	    
	    $this->sHashCode=$this->session->userdata("WEB_AuthorizedKey");
	    
	    $this->Currencyrate	 =$this->session->userdata("WEB_Currencyrate");
	    $this->internationrate=$this->session->userdata("WEB_Currencyrate2");
	    
	    $this->mainCurrency	=$this->session->userdata("WEB_mainCurrency");
	    $this->weblang		=$this->session->userdata("lang");
	    $this->Servercharge  =$this->session->userdata("WEB_Servercharge");
	    $this->AgentCode	=$this->session->userdata("WEB_AgentCode");
	    $this->Ccommision	=$this->session->userdata("WEB_Ccommision");
	   
	    
	    $this->isset_rebate  =(boolean)$this->session->userdata("isrebate");
	    $this->iAirportFee=$this->iAirportFee*$this->Currencyrate;
	    
	    $this->load->model("Huitrip_model");
	   // $this->db2	=$this->load->database('ibe', TRUE);
	    ini_set("date.timezone","Asia/Shanghai");
	}
	
	
	//航班查询并返回对应的航班数据
	//$isPFD表示是否使用私有运价
	public function avflight($sFromport,$sDestport,$dFlightdate,$sAirline,$isback=false,$FlightType,$isPFD="FD")
	{
		
		$ArrayPolicy=$this->Huitrip_model->ws_getHuitripPolicy($sFromport,$sDestport,$dFlightdate,$FlightType);
		
		$arFlight=array();
		
		//获取AV数据
		$lstFlight=$this->ws_avflight($sFromport,$sDestport,$dFlightdate,$sAirline);
		$iFlight=count($lstFlight);
		
		//获取FD数据,私有运价不需要获取FD数据		
		$ArrayFD=$this->ws_FDdata($sFromport,$sDestport,$dFlightdate,$isback,substr($isPFD,0,2)=="PF");
		
		
		//获取私有运价数据
		$ArrayPFD=$this->getPrivatefreight($sFromport,$sDestport,$dFlightdate,$FlightType,$isback,"A");
				
		
		//输出航班详细信息
		for($i=0;$i<$iFlight;$i++)
		{
			$item=explode("|",$lstFlight[$i]);
			//FM9370|20110510|0655|0920|true|738|0|F8,P2,A2,YA,KA,BA,HA,LA,MA,XA,I5,|SZX|SHA
			
			if($sAirline==substr($item[0],0,2) ||$sAirline=="")
			{
				//回程航班查询的话必须符合去程信息				
				if(!$isback || ($isback && $item[8]==$sFromport && $item[9]==$sDestport))
				{
					
					$flightdata=array("ClassList" => $item[7],
							"flightnum"=> $item[0],
							"FlightType"=> $FlightType,
							"FlightDate" => $dFlightdate,
							"isback" => $isback,
							"goClass"	=> "",
							"goMix"	=> TRUE
							);
							
					$ClassData=$this->getClassPrice($ArrayFD,$ArrayPFD,$ArrayPolicy,$flightdata);
					$iOilfee=$this->getOilFee($ArrayFD["TPM"])*$this->Currencyrate;
					$iOilfee=ceil($iOilfee);

					//计算运价
					$arFlight[$i]=array("FlightNo" => $item[0],
									"Airline" => substr($item[0],0,2),
									"flightdate" => $item[1],
									"Depart" => implode(str_split($item[2],2),":"),
									"Arrival" => implode(str_split($item[3],2),":"),
									"Plane" => $item[5],
									"Stop" => $item[6],
									"class" => $ClassData,
									"fromport" => $item[8],
									"destport" => $item[9],
									"fromportName" => $this->getAirportname_buffer($item[8]),
									"destportName" => $this->getAirportname_buffer($item[9]),
									"OilFee" => $iOilfee,
									"AirportFee" => ceil($this->iAirportFee),
									"mainCurrency" => $this->mainCurrency
								);
				}
			}
			
		}
		
		return $arFlight;
		
	}
	
		//航班查询并返回对应的航班数据
	//$isPFD表示是否使用私有运价
	public function avinternationalflight($sFromport,$sDestport,$dFlightdate,$argoData)
	{
		$sAirline		=$argoData["sAirline"];
		$isback		=$argoData["isback"];
		$FlightType    =$argoData["FlightType"];
		$isPFD	     =$argoData["isPFD"];
		$goClass		=$argoData["goClass"];
		$goMix		=$argoData["goMix"];
		
		$ArrayPolicy=array();
		$arFlight=array();
		
		//国际航班使用国际运价换算费率
		$this->Currencyrate=$this->internationrate;
		
		//获取AV数据
		$lstFlight=$this->ws_avflight($sFromport,$sDestport,$dFlightdate,$sAirline);
		$iFlight=count($lstFlight);
		
		//获取FD数据,私有运价不需要获取FD数据
		$ArrayFD=array("TPM"=>0,"ClassYPrice" =>0);
		
		//获取税费数据，
		$ArrayTax=$this->getTaxbydb($sFromport,$sDestport,$dFlightdate);
		
		
		//获取私有运价数据
		$ArrayPFD=$this->getPrivatefreight($sFromport,$sDestport,$dFlightdate,$FlightType,$isback,"I");
		
				
		
		//输出航班详细信息
		for($i=0;$i<$iFlight;$i++)
		{
			$item=explode("|",$lstFlight[$i]);
			//FM9370|20110510|0655|0920|true|738|0|F8,P2,A2,YA,KA,BA,HA,LA,MA,XA,I5,|SZX|SHA
			
			if($sAirline==substr($item[0],0,2) ||$sAirline=="")
			{
				//回程航班查询的话必须符合去程信息				
				if(!$isback || ($isback && $item[8]==$sFromport && $item[9]==$sDestport))
				{
					
					$flightdata=array("ClassList" => $item[7],
								"flightnum"=> $item[0],
								"FlightType"=> $FlightType,
								"FlightDate" => $dFlightdate,
								"isback" => $isback,
								"goClass"	=> $goClass,
								"goMix"	=> $goMix
							);
							
					$ClassData=$this->getClassPrice($ArrayFD,$ArrayPFD,$ArrayPolicy,$flightdata);
					$iOilfee=$this->getInternationalTax($ArrayTax,substr($item[0],0,2))*$this->Currencyrate;

					//计算运价
					$arFlight[$i]=array("FlightNo" => $item[0],
									"Airline" => substr($item[0],0,2),
									"flightdate" => $item[1],
									"Depart" => implode(str_split($item[2],2),":"),
									"Arrival" => implode(str_split($item[3],2),":"),
									"Plane" => $item[5],
									"Stop" => $item[6],
									"class" => $ClassData,
									"fromport" => $item[8],
									"destport" => $item[9],
									"fromportName" => $this->getAirportname_buffer($item[8]),
									"destportName" => $this->getAirportname_buffer($item[9]),
									"OilFee" => ceil($iOilfee),
									"AirportFee" => ceil($this->iAirportFee),
									"mainCurrency" => $this->mainCurrency
								);
				}
			}
			
		}
		
		return $arFlight;
		
	}
	//从数据库获取税费数据
	public function getTaxbydb($sFromport,$sDestport,$dFlightdate)
	{
		$this->db->select("airline,Tax,Taxchd ");
		$this->db->like("Fromport",$sFromport);
		$this->db->like("Destport",$sDestport);
		$this->db->where("Enabled",1);
		$this->db->where("AgentCode",$this->AgentCode);
		$this->db->where("DateDiff('".Date("Y-m-d",strtotime($dFlightdate))."',FlightDate1)>=0");
		$this->db->where("DateDiff('".Date("Y-m-d",strtotime($dFlightdate))."',FlightDate2)<=0");
		
		$this->db->where("DateDiff('".Date("Y-m-d")."',Effectdate1)>=0");
		$this->db->where("DateDiff('".Date("Y-m-d")."',Effectdate2)<=0");
		
		
		$this->db->order_by("id","desc");
	
		$query=$this->db->get("inf_internationtax");
		
		if ($query->num_rows()>0)
		{
			return $query->result();
		}else
		{
			
			$this->db->select("airline,Tax,Taxchd ");
			$this->db->like("Fromport",$sFromport);
			$this->db->like("Destport",$sDestport);
			$this->db->where("Enabled",1);
			$this->db->where("AgentCode","system");
			$this->db->where("DateDiff('".Date("Y-m-d",strtotime($dFlightdate))."',FlightDate1)>=0");
			$this->db->where("DateDiff('".Date("Y-m-d",strtotime($dFlightdate))."',FlightDate2)<=0");
			
			$this->db->where("DateDiff('".Date("Y-m-d")."',Effectdate1)>=0");
			$this->db->where("DateDiff('".Date("Y-m-d")."',Effectdate2)<=0");
			
			
			$this->db->order_by("id","desc");
		
			$query=$this->db->get("inf_internationtax");
			
			if ($query->num_rows()>0)
			{
				return $query->result();
			}
			
			return array();			
		}
	}
	
	//查询国际航班运价
	public function getInternationalTax($ArrayTax,$airline)
	{
		
		foreach($ArrayTax as $item)
		{
			if($item->airline==$airline)	
			{
				return $item->Tax;
			}
		}
		return -1;
	}
	
	//根据航班查询运价
	public function ws_queryiTaxbyflight($arTaxdata)
	{
		
	   	$sFlightInfo=$arTaxdata["fromport"]."|".$arTaxdata["destport"]."|".
		   			$arTaxdata["FlightNo"]."|".$arTaxdata["class"]."|".
		   			$arTaxdata["flightdate"];
				
		
		$client = new SoapClient($this->wsdl);  
		$param = array('FlightInfo'=> $sFlightInfo,
				"sHashcode"=> $this->sHashCode
				);

		$out = $client->QTEdata($param); 
		$qtedata = $out->QTEdataResult;
	
		
		$xml = simplexml_load_string($qtedata);
		
		$result = $xml->xpath("FNs/FN/Item");
		
		$Tax=0;
		foreach ($result as $aitem)
	  	{
		  	$aitem=(array)$aitem;
		  	if($aitem["@attributes"]["Type"]=="T")
		  	{
		  		$Tax+=(double)$aitem["@attributes"]["Value"];
		  	}
		 
	  	}
		$Tax=ceil($Tax*0.8335*$this->Currencyrate*$this->internationrate);
		
		//处理SHA的
		if(strpos("SHA/PVG",$arTaxdata["fromport"])>-1)
		{
			$arTaxdata["fromport"]="SHA/PVG";
		}
		
		if(strpos("SHA/PVG",$arTaxdata["destport"])>-1)
		{
			$arTaxdata["destport"]="SHA/PVG";
		}
		
		//删除到本地数据s
		$deltaxdata=array(
			"Fromport" => $arTaxdata["fromport"],
			"Destport" => $arTaxdata["destport"],
			"Flightdate1" => $arTaxdata["flightdate"],
			"Flightdate2" => $arTaxdata["flightdate"],
			"airline" => substr($arTaxdata["FlightNo"],0,2),
			"AgentCode" => "system",
			"AgentName" => "system"
		);
		$this->db->delete("inf_internationtax",$deltaxdata);
		
		$sPolicyGroup=uniqid("T");
		//保存到本地数据s
		$insertaxdata=array(
			"PolicyGroup "	=> $sPolicyGroup,
			"Fromport" => $arTaxdata["fromport"],
			"Destport" => $arTaxdata["destport"],
			"Flightdate1" => $arTaxdata["flightdate"],
			"Flightdate2" => $arTaxdata["flightdate"],
			"airline" => substr($arTaxdata["FlightNo"],0,2),
			"Tax" => $Tax,
			"Taxchd" => $Tax,
			"Entrydate1" => date("Y-m-d G:i"),
			"Effectdate1" => date("Y-m-d G:i"),
			"Effectdate2" => DateAdd("m",1,date("Y-m-d G:i")),
			"Enabled" => 1,
			"AgentCode" => "system",
			"AgentName" => "system"
		);
		
		$this->db->insert("inf_internationtax",$insertaxdata);
		
		return $Tax;	
	   
	   
	}
	
	
	//从缓存获取港口信息
	public function getAirportname_buffer($airportcode)
	{
		if(array_key_exists($airportcode,$this->arAirport))
		{
			//echo "我没访问数据库<br>";
			return $this->arAirport[$airportcode];
		}else
		{
			$name=$this->getAirportname($airportcode);			
			
			if($name!="")
			{
				$this->arAirport[$airportcode]=$this->getNameBylang($name->EName.'-'.$name->CName.'-'.$name->GName);	
			}else
			{
				$this->arAirport[$airportcode]="";
			}
			
			return $this->arAirport[$airportcode];
		}
		
	}
	//根据语言获取港口名称
	public function getNameBylang($airportName)
	{
		$lang=$this->weblang;
		$arName=explode("-",$airportName);
		
		if($lang=="english")
		{
			return $arName[0];
		}else if($lang=="sc")
		{
			return $arName[1];
		}else if($lang=="zh")
		{
			return $arName[2];
		}else
		{
			return $arName[0];
		}
	}
	
	//获取港口名称
	protected function getAirportname($airportcode)
	{
		$this->db->where("Airportcode",$airportcode);
		$query=$this->db->get("knb_airports");
		//echo "访问了一次数据库<br>";
		if($row=$query->row())
		{
			return $row;
		}
		return "";
	}
	//转换预订字符串为数组对象
	public function getarrbookinfo($bookdata,$backdata="")
	{
		//0:MU|1:20110517|MU5368|07:50|09:50|SZX|PVG|320|0|110|50|T|>9|630.00|
		//0:MU
		//1:20110517
		//2:MU5368
		//3:07:50
		//4:09:50
		//5:SZX
		//6:PVG
		//7:320
		//8:0
		//9:110
		//10:50
		//11:T
		//12:>9
		//13:630.00
		//14:PF1/PF0/FD
		//15:TGQNote
		
		//16:返点后的价格 628.8
		//17:汇游政策ID AP123
		//18:汇游政策返点 3.5
		
		$ardata[0]=$bookdata;
		
		if($backdata!="")
		{
			$ardata[1]=$backdata;
		}
		$iCount=count($ardata);
		
		for($i=0;$i < $iCount;$i++)
		{
			$item=explode("|",$ardata[$i]."|FD|0|ID|0");
			$item[12]=str_replace(">","",$item[12]);
			
			if($item[16]==0)
			{
				$item[16]=$item[13];
			}
			
			$ardata[$i]=array("FlightNo" => $item[2],
				"Airline" => $item[0],
				"flightdate" => $item[1],
				"Depart" => $item[3],
				"Arrival" => $item[4],
				"Plane" => $item[7],
				"Stop" => $item[8],
				"class" => array(array($item[11],$item[12],$item[13])),
				"fromport" => $item[5],
				"destport" => $item[6],
				"OilFee" => $item[9],
				"AirportFee" => $item[10],
				"fromportName" => $this->getAirportname_buffer($item[5]),
				"destportName" => $this->getAirportname_buffer($item[6]),
				"PFD" => $item[14],
				"PFDID" => $item[15],
				"cost" => $item[16],
				"poicyid" => $item[17],
				"rebate" => $item[18]
			);
		}
		
			
		return $ardata;
		
	}
	//航班查询
	public function ws_avflight($sFromport,$sDestport,$dFlightdate,$sAirline)
	{
		/*
		$request=$this->IBE_SITE.'vIBEService.asmx/AVFlight?';
		$request=$request.'sHashcode='.$this->sHashCode.
				'&Fromport='.$sFromport.
				'&Destport='.$sDestport.
				'&FlightDate='.$dFlightdate.
				'&Airline='.$sAirline;
		
		$avcontext=file_get_contents($request);
		//$avcontext=file_get_contents(base_url().'js/testav.txt');
		
		$avcontext=str_ireplace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=\"http://ws.jzinfo.sh.cn/\">","",$avcontext);
		$avcontext=str_ireplace("</string>","",$avcontext);
		*/
		
		$client = new SoapClient($this->wsdl);  
		$param = array('Fromport'=> $sFromport,
				'Destport'=> $sDestport,
				'FlightDate'=> $dFlightdate,
				"sHashcode"=> $this->sHashCode
				);

		$out = $client->AVFlight($param); 
		$arResult = $out->AVFlightResult;
		
		
		$lstFlight=explode("-",$arResult);		
		
		return $lstFlight;

	}
	
	//执行FD指令
	protected function ws_FDdata($sFromport,$sDestport,$dFlightdate,$isback,$isPFD)
	{
		/*
		$request=$this->IBE_SITE.'vIBEService.asmx/FDdata?';
		$request=$request.'sHashcode='.$this->sHashCode.
				'&Fromport='.$sFromport.
				'&Destport='.$sDestport.
				'&FlightDate='.$dFlightdate.
				'&Airline=';
		
		$fdcontext=file_get_contents($request);	//读取远程数据
		*/
		$client = new SoapClient($this->wsdl);  
		$param = array('Fromport'=> $sFromport,
				'Destport'=> $sDestport,
				'FlightDate'=> $dFlightdate,
				'Airline'=> "",
				"sHashcode"=> $this->sHashCode
				);

		$out = $client->FDdata($param); 
		$fdcontext = $out->FDdataResult;
		
		//$fdcontext=file_get_contents(base_url().'js/testfd.xml');	//读取本地测试数据
		if(strpos($fdcontext,"无法识别的主机错误信息")>0)
		{
			return array("TPM"=>1000,"ClassYPrice" =>0);
		}
		
		/*
		$fdcontext=str_ireplace("&lt;","<",$fdcontext);
		$fdcontext=str_ireplace("&gt;",">",$fdcontext);
		$fdcontext=str_ireplace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=\"http://ws.jzinfo.sh.cn/\">","",$fdcontext);
		$fdcontext=str_ireplace("</string>","",$fdcontext);
		*/
		
		$xml = simplexml_load_string($fdcontext);
		
		
		$attr=(array)$xml->attributes();
		//print_r($attr);
		$FDdata['TPM']=$attr['@attributes']['TPM'];
		$FDdata['ClassYPrice']=$attr['@attributes']['ClassYPrice'];
		
		//使用私有运价不需要查公有运价
		if($isback && $isPFD)
		{
			return $FDdata;
		}
		foreach ($xml->children() as $child)
	  	{
	  		$aitem=(array)$child->children();	//转换成"key->value"形式
	  		//形如[CA-F]=2800
		  	$FDdata[$aitem['Carrier'].'-'.$aitem['Farebase']]=(double)$aitem['Price'];
	  	}
	  	
	  	return $FDdata;
	}
	
	//计算舱位运价
	protected function getClassPrice($FDarray,$PFDarray,$ArrayPolicy,$flightdata)
	{
		$ClassList	=$flightdata["ClassList"];
		$airline       =substr($flightdata["flightnum"],0,2);
		$flightnum     =$flightdata["flightnum"];
		$FlightType    =$flightdata["FlightType"];
		$FlightDate	=$flightdata["FlightDate"];		
		$isback		=$flightdata["isback"];
		
		$goClass		=$flightdata["goClass"];
		$goMix		=$flightdata["goMix"];
		
		//F8,P2,A2,YA,KA,BA,HA,LA,MA,XA,I5,
		$arclass=explode(",",$ClassList);
		$arPrice=array();
		
		//item形如YA
		foreach($arclass as $item)
		{
			$iPrice=0;
			$sclass=substr($item,0,1);
			$iseat=substr($item,1,1);
			$pricetype="FD";
			$policyid=0;
			if($iseat=="A" || is_numeric($iseat))
			{
				//先从FD数据中找
				if(array_key_exists($airline."-".$sclass,$FDarray))
				{
					$iPrice=$FDarray[$airline."-".$sclass];
					$pricetype="FD";
				}else
				{
					//没有的话匹配私有运价
					$cFlightdata=array(
						"airline"		=> $airline,                
						"sclass"		=> $sclass,               
						"YPrice"		=> $FDarray['ClassYPrice'],
						"flightnum"	=> $flightnum,           
						"FlightType"	=> $FlightType,           
						"isback"		=> $isback,                 
						"goMix"		=> $goMix,
						"goClass"		=> $goClass
					);
		
		
					$arPrivate=$this->getPriceByPFD($PFDarray,$cFlightdata);
				;
					$iPrice	=$arPrivate[0];
					if($iPrice>0)
					{
						$pricetype="PF".$arPrivate[3].$arPrivate[4];
						$policyid=$arPrivate[1];	
						//国际航线使用自定义运价
						if($arPrivate[3]=="I")
						{
							$this->iAirportFee	=0;
							$this->iinter		=$arPrivate[2];	
						}
						$i=0;
					}
				}
				if($iseat=="A"){$iseat=">9";}
					
			}
			if($iPrice>0)
			{
				
				//再计算汇游返点
				$tpolicy=$this->Huitrip_model->hy_getPrice($ArrayPolicy,$sclass,$airline,$FlightType,$iPrice,$FlightDate,$flightnum);
				$tpolicy[0]=$this->getFarebycharge($tpolicy[0]);
				
				$iPrice=$this->getFarebycharge($iPrice);
				$iPrice=ceil($iPrice);
				
				//舱位，数量，票面价
				array_push($arPrice,array($sclass,$iseat,$iPrice,$pricetype,$policyid,$tpolicy));
			}
		}
		
		usort($arPrice,"my_sort");
		//print_r($arPrice);
		return $arPrice;
	}
	
	//计算燃油费,$tmp为公里数
	protected function getOilFee($TPM=900)
	{
		//国际航线税费
		if($this->iinter>0)
		{
			
			return $this->iinter;
		}
		if($TPM>800)
		{
			return $this->iOilFee;
		}
		else
		{
			return $this->iOilFee/2+($this->iOilFee/2)%10;
		}
	}
	//产生PNR
	public function  ws_CreatePNR($Tel,$arFlightInfo,$arPassenger,$RMK)
	{
		return explode(",","0000,XXXXX,HKG261");
		
		
		//SZX,SHA^SHA,SZX^ZH9815,ZH9814^Y,V^2010-11-13,2010-11-17^17:10
		
		$flightxml="";
		$icount=count($arFlightInfo);
		for($i=0;$i< $icount;$i++)
		{
			$arFlightInfo[$i]["departtime"]=str_replace(":","",$arFlightInfo[$i]["departtime"]);
			
			$flightxml.='<flight>'.
				'<where fromport="'.$arFlightInfo[$i]["fromport"].'" destport="'.$arFlightInfo[$i]["destport"].'" ClassCode="'.$arFlightInfo[$i]["classcode"].'"  />'.
				'<when flightdate="'.$arFlightInfo[$i]["flightdate"].'" departtime="'.$arFlightInfo[$i]["departtime"].'"  Carrier="'.substr($arFlightInfo[$i]["flightno"],0,2).'" flightno="'.$arFlightInfo[$i]["flightno"].'" />'.
				'</flight>';
		}
		
		$passengerxml="";
		foreach($arPassenger as $item)
		{
			$passengerxml.='<Passenger Name="'.$item["Name"].'" IDType="NI"'.
				 ' CardNo="'.$item["CardNo"].'" Country="'.$item["Nationality"].'"'.
				 ' BirthDay="'.$item["PI_userBirth"].'" Gender="'.$item["PI_userGender"].'" idExpiration="'.$item["PI_idExpiration"].'"  '.
				 ' MobilePhone="'.$item["PassengerTel"].'" />';
		}
		
		
		
		$pnrxml='<?xml version="1.0" encoding="utf-8"?>'.
				'<PNRDATA><cmd>Create</cmd>'.
				'<TicketLimitDate></TicketLimitDate>'.
				'<TicketLimitTime></TicketLimitTime>'.
				'<Telephone>'.$Tel.'</Telephone>'.
				'<RMKS>'.$RMK.'</RMKS>'.
				'<vflightGroup>'.$flightxml.				
				'</vflightGroup>'.
				'<vPassenger>'.
				$passengerxml.
				'</vPassenger>'.
				'<OtherSegments><OtherSegment></OtherSegment></OtherSegments></PNRDATA>';
		//echo $pnrxml;
		
		//exit();
		
		$client = new SoapClient($this->wsdl);  
		$param = array('pnrXML'=> $pnrxml,
				"sHashcode"=> $this->sHashCode
				);

		$out = $client->CreatePNR($param); 
		$pnrcontext = $out->CreatePNRResult;
	
		return explode(",",$pnrcontext);		
		
	}
	
	//计算服务费在票面价基础上加价
	public function getFarebycharge($Fare)
	{
		//return $Fare;
		$Fare=(double)$Fare;
		if($Fare<999)
		{
			return $Fare*$this->Currencyrate+(double)$this->Servercharge[0];
		}else if($Fare<1999)
		{
			return $Fare*$this->Currencyrate+(double)$this->Servercharge[1];
		}else if($Fare<3000)
		{
			return $Fare*$this->Currencyrate+(double)$this->Servercharge[2];
		}else
		{
			return $Fare*$this->Currencyrate+(double)$this->Servercharge[3];
		}
	}
	//获取私有运价
	public function getPrivatefreight($Fromport,$Destport,$FlightDate,$FlightType,$isback,$ProductType)
	{
		$this->load->model("Policy_model");
		
		if($FlightType=="2")
		{
			$ClassType="R";
			if($isback)
			{
				$tmpPort=$Fromport;
				
				$Fromport=$Destport;
				$Destport=$tmpPort;
			}
		}else
		{
			$ClassType="S";
		}
		$weekarray=array("7","1","2","3","4","5","6");
		
		$querydata=array("FromCityCode"=> $Fromport,
			"DestCityCode"=> $Destport,
			"SalesDate1"=> date("Y-m-d"),
			"SalesDate2"=> "",
			"FlightDate1"=> $FlightDate,
			"FlightDate2"=> "",
			"ClassType" => $ClassType,
			"isSleep" => "0",
			"Week1"=> $weekarray[date("w",strtotime($FlightDate))],
			"ProductType" => $ProductType
		);
		
		//print_r($querydata);
		$userdata	=array("uadmin_account" => $this->AgentCode);
		
		
		$result=$this->Policy_model->queryPolicy($querydata,$userdata);
		//print_r($result);
		return $result[1];
	}
	//计算私有运价
	protected function getPriceByPFD($PFDarray,$Flightdata)
	{
		
		
		$airline	  =$Flightdata["airline"];
		$sclass     =$Flightdata["sclass"];
		$YPrice     =$Flightdata["YPrice"];
		$flightnum  =$Flightdata["flightnum"];
		$FlightType =$Flightdata["FlightType"];
		$isback     =$Flightdata["isback"];
		$goMix      =$Flightdata["goMix"];
		$goClass	  =$Flightdata["goClass"];
		
		$flightnum=str_replace($airline,"",$flightnum);

		
		
		foreach($PFDarray as $item)
		{
			//形如[CA-F]=2800
			if($item->Airline==$airline && $item->ClassCode==$sclass)
			{
				if($item->FlightNum1=="" || 
				 (substr($item->FlightNum1,0,1)=="!" && !strpos($item->FlightNum1,$flightnum)>-1) ||
				 (substr($item->FlightNum1,0,1)!="!" && strpos($item->FlightNum1,$flightnum)>-1)
				){
					
					if($item->Fare>0){
						
						//国际运价
						if($item->ProductType=="I")
						{
							
							$iFare=$item->Fare;
							//来回程的话价格为一半
							if($FlightType=="2")
							{
								//回程匹配混合舱位或者不允许混合舱位
								if($isback)
								{
									//echo $item->MixClass,$goMix;
									//去程不允许混舱，回程也必须是该舱位
									//或者去程是混舱，回程也能混舱
									if((!$item->MixClass && !$goMix  && $goClass==$sclass) ||
										($item->MixClass && $goMix)
									)
									{
										$iFare=$item->Fare/2;
									}else
									{
										continue;
									}
									
								}else
								{
									$iFare=$item->Fare/2;
								}
							}
							
							return array($iFare,$item->PolicyID,$item->TaxFee,$item->ProductType,$item->MixClass);
						}else
						{
							return array($item->Fare,$item->PolicyID,$item->TaxFee,$item->ProductType,$item->MixClass);
						}
						
					}else
					{
						return array($this->getDiscountFare($item->Discount*0.01*$YPrice),$item->PolicyID,0,$item->ProductType,$item->MixClass);	
					}
					
				}
			}
		}
		return array(0,0);
	}
	//计算折扣
	protected function getDiscountFare($iFare)
	{
		if ($iFare>=(5+ceil($iFare/10)*10))
		{
			$iFare = ceil($iFare/10) * 10 + 10;
		}else
		{
			$iFare = ceil($iFare/10) * 10;
		}
		return $iFare;
	}
}

//排序自定义函数
function my_sort($a, $b)
{
	if ($a[2] == $b[2]) return 0;
	return ($a[2] < $b[2]) ? -1 : 1;
}


?>